package com.appchina.autism.express;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.appchina.autism.util.SoundPoolEngine;
import com.appchina.autism.util.VibratorEngine;
import com.appchina.autism.widget.ItemView;
import com.umeng.analytics.MobclickAgent;

public class DetailActivity extends Activity implements OnClickListener{
	
	private static final String INDEX = "index";
	private static final String TITLE = "title";
	
	private int index;
	private String title;
	
	private int [] indexViewIds = {R.id.detail_1, R.id.detail_2, 
			R.id.detail_3, R.id.detail_4, R.id.detail_5, R.id.detail_6, 
			R.id.detail_7, R.id.detail_8, R.id.detail_9, R.id.detail_10};
	
	private ArrayList<ItemView> indexViews;
	
	private String [] indexStrs;
	
	private int len;
	
	ArrayList<Integer> mSoundIdList;
	SoundPoolEngine soundPoolEngine;
	VibratorEngine vibratorEngine;
	
	private TextView mTitle;
	private View mActionBarLeft;
	private View mActionBarRight;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		soundPoolEngine = SoundPoolEngine.getInstance(this);
		vibratorEngine = VibratorEngine.getInstance(this);
		
		Intent intent = getIntent();
		index = intent.getIntExtra(INDEX, 0);
		title = intent.getStringExtra(TITLE);
		
		
		switch(index) {
		case 0:
			indexStrs = getResources().getStringArray(R.array.express_feel);
			break;
		case 1:
			indexStrs = getResources().getStringArray(R.array.express_desire);
			break;
		case 2:
			indexStrs = getResources().getStringArray(R.array.express_love);
			break;
		case 3:
			indexStrs = getResources().getStringArray(R.array.express_greetings);
			break;
		case 4:
			indexStrs = getResources().getStringArray(R.array.express_weather);
			break;
		case 5:
			indexStrs = getResources().getStringArray(R.array.express_festival);
			break;
		}
		
		len = indexStrs.length;
		
		
		setContentView(R.layout.activity_detail);
		
		mTitle = (TextView) findViewById(R.id.common_widget_action_bar_title);
		mTitle.setText(title);
		mActionBarLeft = findViewById(R.id.common_widget_action_bar_left);
		mActionBarLeft.setOnClickListener(this);
		mActionBarRight = findViewById(R.id.common_widget_action_bar_right);
		mActionBarRight.setOnClickListener(this);
		
		indexViews = new ArrayList<ItemView>();
		for(int i = 0; i < indexViewIds.length; ++i) {
			indexViews.add((ItemView)findViewById(indexViewIds[i]));
		}
		
		for(int i = 0; i < indexViews.size(); ++i) {
			if(i < len) {
				indexViews.get(i).setContent(AutismConstant.imgResIds[index][i], indexStrs[i]);
				indexViews.get(i).setOnClickListener(this);
			} else {
				if((i%2 == 1) && i == len) {
					//填充用
					indexViews.get(i).setContent(AutismConstant.imgResIds[index][0], indexStrs[0]);
					indexViews.get(i).setVisibility(View.INVISIBLE);
				} else {
					indexViews.get(i).setVisibility(View.GONE);
				}
			}
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}
	

	@Override
	protected void onResume() {
		switch(index) {
		case 0:
			int [] soundIds = {R.raw.s2_11, R.raw.s2_12, R.raw.s2_13, 
					R.raw.s2_14, R.raw.s2_15, R.raw.s2_16,
					R.raw.s2_17 };
			mSoundIdList = soundPoolEngine.loadRes(soundIds);
			break;
		case 1:
			int [] soundIds1 = {R.raw.s2_21, R.raw.s2_22, R.raw.s2_23, 
					R.raw.s2_24, R.raw.s2_25, R.raw.s2_26};
			mSoundIdList = soundPoolEngine.loadRes(soundIds1);
			break;
		case 2:
			int [] soundIds2 = {R.raw.s2_31, R.raw.s2_32, R.raw.s2_33, 
					R.raw.s2_34, R.raw.s2_35 };
			mSoundIdList = soundPoolEngine.loadRes(soundIds2);
			break;
		case 3:
			int [] soundIds3 = {R.raw.s2_41, R.raw.s2_42, R.raw.s2_43,
					R.raw.s2_44, R.raw.s2_45, R.raw.s2_46 };
			mSoundIdList = soundPoolEngine.loadRes(soundIds3);
			break;
		case 4:
			int [] soundIds4 = {R.raw.s2_51, R.raw.s2_52, R.raw.s2_53, 
					R.raw.s2_54, R.raw.s2_55, R.raw.s2_56 };
			mSoundIdList = soundPoolEngine.loadRes(soundIds4);
			break;
		case 5:
			int [] soundIds5 = {R.raw.s2_61, R.raw.s2_62, R.raw.s2_63, 
					R.raw.s2_64, R.raw.s2_65, R.raw.s2_66 };
			mSoundIdList = soundPoolEngine.loadRes(soundIds5);
			break;
		}
		MobclickAgent.onResume(this);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		soundPoolEngine.release();
		MobclickAgent.onPause(this);
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.common_widget_action_bar_left:
			DetailActivity.this.finish();
			break;
		case R.id.common_widget_action_bar_right:
			SettingActivity.launch(DetailActivity.this);
			break;
		case R.id.detail_1:
		case R.id.detail_2:
		case R.id.detail_3:
		case R.id.detail_4:
		case R.id.detail_5:
		case R.id.detail_6:
		case R.id.detail_7:
		case R.id.detail_8:
		case R.id.detail_9:
		case R.id.detail_10:
			int index = 0;
		    for(int i = 0; i < indexViewIds.length; ++i) {
		    	if(v.getId() == indexViewIds[i]) {
		    		index = i;
		    		break;
		    	}
		    }
		    soundPoolEngine.play(mSoundIdList.get(index));
			vibratorEngine.vibrate();
			break;
		default:
			break;
		}
	}
	
	public static void launch(Context context, int index, String title) {
		
		Intent intent = new Intent();
		intent.setClass(context, DetailActivity.class);
		intent.putExtra(INDEX, index);
		intent.putExtra(TITLE, title);
		context.startActivity(intent);
		
	}
	

}
